<?php
define("FENGTOPsso_DIR", dirname(__FILE__));
require_once FENGTOPsso_DIR.'/config/global.ini.php';
require_once FENGTOPsso_DIR.'/database/dbmysql.php';

$Flag = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f');
$installSql = new DB($GLOBALS['_FENGTOPsso']['db']['1']['dbhost'], $GLOBALS['_FENGTOPsso']['db']['1']['dbuser'], $GLOBALS['_FENGTOPsso']['db']['1']['dbpw'], $GLOBALS['_FENGTOPsso']['db']['1']['dbname']);

//用户索引
	for ($i = 0; $i < 16; $i++) {
		$sqlCreatIndex = "CREATE TABLE IF NOT EXISTS `".
		$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."user_index_$Flag[$i]` ( 
		`uid` int(11) NOT NULL auto_increment, 
		`username` varchar(64) NOT NULL, 
		`regdate` int(11) unsigned NOT NULL, 
		PRIMARY KEY  (`uid`), 
		UNIQUE `user` (`username`), 
		KEY `regdate` (`regdate`)) 
		ENGINE=".$GLOBALS['_FENGTOPsso']['db']['1']['engine']." 
		CHARACTER SET utf8 COLLATE utf8_bin";
		$installSql->Query($sqlCreatIndex);
	}

//客户端
	$sqlCreatClient = "CREATE TABLE IF NOT EXISTS `".
	$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."client` ( 
	`client_id` tinyint(4) NOT NULL auto_increment, 
	`domain` varchar(60) NOT NULL, 
	`private_key` char(32) NOT NULL, 
	PRIMARY KEY  (`client_id`), 
	UNIQUE KEY `domain` (`domain`)) 
	ENGINE=".$GLOBALS['_FENGTOPsso']['db']['1']['engine']." 
	CHARACTER SET utf8 COLLATE utf8_bin";
	$installSql->Query($sqlCreatClient);

//忘记密码
	$sqlCreatForgetPwd = "CREATE TABLE IF NOT EXISTS `".
	$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."forget_pwd` ( 
	`id` smallint(6) NOT NULL auto_increment,
	`username` varchar(64) character set utf8 NOT NULL, 
	`code` char(32) NOT NULL, 
	`start_ts` int(11) NOT NULL, 
	`state` tinyint(1) NOT NULL, 
	PRIMARY KEY  (`id`)) 
	ENGINE=".$GLOBALS['_FENGTOPsso']['db']['1']['engine']." 
	CHARACTER SET utf8 COLLATE utf8_bin";
	$installSql->Query($sqlCreatForgetPwd);

//在线用户
	$sqlCreatOnLineUser = "CREATE TABLE IF NOT EXISTS `".
	$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."onlineuser` ( 
	`ticket` char(32) character set utf8 NOT NULL, 
	`username` varchar(64) character set utf8 NOT NULL, 
	`expiry` int(11) unsigned NOT NULL, 
	`data` text character set utf8 NOT NULL, 
	UNIQUE KEY `session_id` (`ticket`), 
	UNIQUE KEY `username` (`username`)) 
	ENGINE=".$GLOBALS['_FENGTOPsso']['db']['1']['engine']." 
	CHARACTER SET utf8 COLLATE utf8_bin";
	$installSql->Query($sqlCreatOnLineUser);

//邮箱认证
	$sqlMailAttestation = "CREATE TABLE IF NOT EXISTS `".
	$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."mail_attestation` ( 
	`username` varchar(64) NOT NULL, 
	`email` varchar(64) NOT NULL,
	`attestationkey` varchar(32) NOT NULL, 
	`regdate` int(11) NOT NULL, 
	PRIMARY KEY  (`email`)) 
	ENGINE=".$GLOBALS['_FENGTOPsso']['db']['1']['engine']." 
	CHARACTER SET utf8 COLLATE utf8_bin";
	$installSql->Query($sqlMailAttestation);
	

//用户明细表
	for ($k2 = 0; $k2 < 16; $k2++) {
		for ($l = 0; $l < 16; $l++) {
			$sqlCreatUserTable = "CREATE TABLE `".
			$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."user_$Flag[$k2]$Flag[$l]`( 
			  `uid` int(11) unsigned NOT NULL AUTO_INCREMENT ,
			  `username` varchar(64) NOT NULL,
			  `password` char(32) NOT NULL,
			  `email` varchar(64) NOT NULL,
			  `user_sex` tinyint(1) NOT NULL,
			  `status` tinyint(1) NOT NULL,
			  `emailstatus` tinyint(1) NOT NULL default '0',
			  `regdate` int(11) unsigned NOT NULL,
			  `regip` varchar(16) NOT NULL,
			  `lastlogintime` int(11) unsigned NOT NULL,
			  `lastloginip` varchar(16) NOT NULL,
			  `question` VARCHAR( 128 ) NOT NULL,
			  `uanswer` VARCHAR( 30 ) NOT NULL,
			  PRIMARY KEY  (`uid`),
			  UNIQUE KEY `user` (`username`)) 
			  ENGINE=".$GLOBALS['_FENGTOPsso']['db']['1']['engine']." 
			  CHARACTER SET utf8 COLLATE utf8_bin";
			$installSql->Query($sqlCreatUserTable);
		}
	}
	
	//邮箱-用户名对照表
	for ($k2 = 0; $k2 < 16; $k2++) {
		for ($l = 0; $l < 16; $l++) {
			$sqlCreatUserMailTable = "CREATE TABLE `".
			$GLOBALS['_FENGTOPsso']['db']['1']['tablepre']."usermail_$Flag[$k2]$Flag[$l]`( 
			  `email` varchar(64) NOT NULL ,
			  `username` varchar(64) NOT NULL,
			  PRIMARY KEY  (`email`)) 
			  ENGINE=".$GLOBALS['_FENGTOPsso']['db']['1']['engine']." 
			  CHARACTER SET utf8 COLLATE utf8_bin";
			$installSql->Query($sqlCreatUserMailTable);
		}
	}
	
	$installSql->Destroy();

?>
